<template>
<div>
  <!--删除dialog-->
  <el-dialog
      v-model="delDialogVisible"
      width="15%"
  >
    <span>此操作无法撤销,确认删除?</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确认删除
        </el-button>
      </span>
    </template>
  </el-dialog>
</div>
</template>

<script setup>
import {ElDialog, ElInput, ElForm, ElFormItem, ElButton, ElMessage} from "element-plus";
import {reactive, ref, defineProps} from "vue";
import {updatefile, delfile, getlist} from "@/api/index.js"
const editDialogVisible = ref(false)
const delDialogVisible = ref(false)
// 表单的ref
const editFileFormRef = ref(false)
const editFileForm = reactive({
  newFileName:''
})
// 当前正在编辑的row
let currentRow = reactive({})
// 校验规则
const editRules = {
  newFileName:[
    { required: true, message: '文件名不能为空', trigger: 'blur' },
  ]
}

let obj

/*父组件事件*/
const emits = defineEmits(['reLoadFileList','getFileLists'])

/*<!--编辑dialog-->*/
const showEditDialog = (row) =>{
  editDialogVisible.value = true
  currentRow = row
}
const onEdit = async (formRef)=>{
  if(!localStorage.getItem('syhyDocManSys')){
    ElMessage({
      message:'请先登录',
      type:'error'
    })
    return router.push('/login')
  }
  obj = JSON.parse(localStorage.getItem('syhyDocManSys'))

  if(currentRow.name === editFileForm.newFileName){
    ElMessage({
      message:'修改后的名称不能与原文件名一致!',
      type:'error'
    })
    return
  }
  const info = {
    fileId:currentRow.id,
    newFileName:editFileForm.newFileName,
    userId:obj.id
  }

  try {
    const v = await formRef.validate()
    if(v){
      const {data:res} = await updatefile(info)
      if(res.code === 200){
        ElMessage({
          message:'修改成功!',
          type:'success'
        })
        editDialogVisible.value = false
        editFileForm.newFileName = ''
        emits('getFileLists',{key:currentRow.id,})
      }
    } else {

    }
  } catch (error){
  ElMessage({
    message:error,
    type:'warning'
  })
}
}


/*<!--删除dialog-->*/
const showDelDialog = (row) =>{
  delDialogVisible.value = true
}

const onDel = (row)=>{

}


/*公共函数*/
const closeDialog = ()=>{
  editDialogVisible.value = false
  delDialogVisible.value = false
}
// 暴露showdialog函数
defineExpose({
  showEditDialog,
  showDelDialog
})
</script>

<style scoped lang="less">

</style>